<template>
  <el-dialog :model-value="show" title="分配角色" @close="emit('hide')">
    <el-form ref="formRef" :model="form" :rules="rules">
      <el-form-item label="角色名称" prop="name">
        <el-input v-model="form.name" autocomplete="off" />
      </el-form-item>
      <el-form-item label="描述">
        <el-input
          v-model="form.description"
          :rows="4"
          type="textarea"
          autocomplete="off"
        />
      </el-form-item>
      <el-form-item label="是否启用">
        <el-radio-group v-model="form.status" class="ml-4">
          <el-radio :label="1">是</el-radio>
          <el-radio :label="0">否</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="emit('hide', true)">取消</el-button>
        <el-button type="primary" @click="submitFn">确定</el-button>
      </span>
    </template>
  </el-dialog>
</template>
<script setup lang="ts">
import { reactive, ref, watch } from "vue";
import { ElMessage, ElMessageBox, FormInstance } from "element-plus";
import { addRoleApi, updateRole } from "./api";
const props = defineProps({
  show: {
    type: Boolean,
    default: false,
  },
  id: {
    type: Number,
    default: 0,
  },
  data: {
    type: Object,
    default: {
      name: "",
      description: "",
      id: 0,
      status: 0,
    },
  },
  //   name: {
  //     type: String,
  //     default: "",
  //   },
  //   description: {
  //     type: String,
  //     default: "",
  //   },
});

watch(
  () => props.id,
  () => {
    if (props.id !== 0) {
      Object.assign(form, props.data);
    } else {
      form.description = "";
      form.name = "";
      form.status = 0;
    }
  }
);

const emit = defineEmits(["hide"]);

const editRole = () => {
  ElMessageBox.confirm("确定要修改当前角色的信息吗？", "修改角色信息", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      updateRole(form).then((res) => {
        if (res.code === 200) {
          ElMessage.success("修改成功");
          formRef.value?.resetFields();
          emit("hide", true);
        }
      });
    })
    .catch(() => {});
};

const addRole = () => {
  ElMessageBox.confirm("确定要添加用户的信息吗？", "添加用户信息", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
  })
    .then(() => {
      addRoleApi(form).then((res) => {
        if (res.code === 200) {
          // 提示
          ElMessage.success("添加成功");
          // 把dialog关闭
          // 刷新数据
          emit("hide");
          // 清空表单
          formRef.value?.resetFields();
        }
      });
    })
    .catch(() => {});
};

const rules = {
  name: [{ required: true, trigger: "blur", message: "请输入角色名称" }],
};

const form = reactive<any>({
  name: "",
  description: "",
  id: 0,
  status: 0,
});

const formRef = ref<FormInstance>();

const submitFn = () => {
  // 还要再做一次数据的校验
  formRef.value?.validate((ok: boolean) => {
    if (ok) {
      if (props.id === 0) {
        // 如果是添加
        addRole();
      } else {
        // 编辑
        editRole();
      }
    } else {
      ElMessage.error("取消成功");
    }
  });
};
</script>
<style lang="less" scoped></style>
